package weaver.interfaces.dsjj.duss.util;

import weaver.general.StaticObj;
import weaver.interfaces.datasource.DataSource;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.zip.ZipInputStream;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-07-06
 * @Description: ${description}
 * @ModifyLog:
 **/

public class DocToBinary {

    public static void queryMailFile(String filepath,String iszip) throws Exception {
        DataSource ds = (DataSource) StaticObj.getServiceByFullname(("datasource.local"), DataSource.class);
        Connection conn = null;
        try {
            conn = ds.getConnection();
            String sql = " update mail_middleinfo set filecontent=? where id=? ";
            PreparedStatement pstm = conn.prepareStatement(sql);
                InputStream inputstream = null;
                ZipInputStream zin = null;
                try {
                    File docfile = new File(filepath);
                    if ("0".equals(iszip)) {
                        inputstream = new BufferedInputStream(new FileInputStream(docfile));
                    } else {
                        zin = new ZipInputStream(new FileInputStream(docfile));
                        if (zin.getNextEntry() != null) {
                            inputstream = new BufferedInputStream(zin);
                        }
                    }
                    pstm.clearParameters();
                    pstm.setBinaryStream(1, inputstream);
                    pstm.addBatch();
                    if (zin != null) {
                        zin.close();
                    }

                    if (inputstream != null) {
                        inputstream.close();
                    }
                } catch (Exception e) {
                } finally {
                    if (inputstream != null) {
                        inputstream.close();
                    }

                    if (zin != null) {
                        zin.close();
                    }
                }
            pstm.executeBatch();
            conn.close();
        } catch (Exception e) {
        } finally {
            try {
                conn.close();
            } catch (Exception e) {

            }
        }
    }

}
